﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheControllerRisk.Settings;

namespace RiskMan.Config
{
    class WorkingTimeClass
    {
        private DateTime _start;
        private DateTime _end;
        private bool dayKliring;


        public WorkingTimeClass(String account)
        {
            dayKliring = false;
            InitializeTime(account);
        }

        public bool IsWork()
        {
            bool result = false;

            if (DateTime.Now >= _start && DateTime.Now <= _end)
            {
                result = true;

                if (dayKliring == true)
                    if (DateTime.Now > RiskMan.Properties.Settings.Default.StartDay && DateTime.Now < RiskMan.Properties.Settings.Default.EndDay)
                    {
                        result = false;
                    }

                if (DateTime.Now > RiskMan.Properties.Settings.Default.StartNight && DateTime.Now < RiskMan.Properties.Settings.Default.EndNight)
                {
                    result = false;
                }
            }

            return result;
        }


        private bool _isForts(string account)
        {
            bool result = false;

            if (account != null)
                if (account.Substring(0, 5) == "SPBFU")
                {
                    result = true;
                }

            return result;
        }

        private void InitializeTime(string account)
        {
            _start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                   RiskMan.Properties.Settings.Default.StartSessionHH,
                                   RiskMan.Properties.Settings.Default.StartSessionMM,
                                   00
                                 );
            if (_isForts(account) == true || (Properties.Settings.Default.adapter == cfg.adapterAlfa && account == "FORTS"))
            {
                dayKliring = true;
                // фортс
                _end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                     RiskMan.Properties.Settings.Default.StopSessionFortsHH,
                                     RiskMan.Properties.Settings.Default.StopSessionFortsMM,
                                     00
                                   );
            }
            else
            {
                // ммвб
                _end = new DateTime
                (
                    DateTime.Now.Year,
                    DateTime.Now.Month,
                    DateTime.Now.Day,
                    RiskMan.Properties.Settings.Default.StopSessionHH,
                    RiskMan.Properties.Settings.Default.StopSessionMM,
                    00
                );
            }
        }
    }
}
